From: Chun-wei Fan Date: Wed, 10 May 2017 01:25:43 +0000 (-0700) Subject: build/win32/replace.py: Fix replacing items in files with UTF-8 content X-Git-Tag: archive/raspbian/3.24.39-1+rpi1~1^2~65^2~39^2~560 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=801e9cf74baedee0e08eed199ec405af7b9bd521;p=gtk%2B3.0.git build/win32/replace.py: Fix replacing items in files with UTF-8 content Some files that this script will process might have UTF-8 items in there, which can cause problems on Python 3.x as it is more strict and careful on unicode issues. Fix this by: -Doing what we did before on Python 2.x -Opening the file with encoding='utf-8' on Python 3.x --- diff --git a/build/win32/replace.py b/build/win32/replace.py index a81bab9426..f215cce584 100644 --- a/build/win32/replace.py +++ b/build/win32/replace.py @@ -21,9 +21,15 @@ valid_actions = ['remove-prefix', 'replace-str', 'remove-str'] +def open_file(filename, mode): + if sys.version_info[0] < 3: + return open(filename, mode=mode) + else: + return open(filename, mode=mode, encoding='utf-8') + def replace_multi(src, dest, replace_items): - with open(src, 'r') as s: - with open(dest, 'w') as d: + with open_file(src, 'r') as s: + with open_file(dest, 'w') as d: for line in s: replace_dict = dict((re.escape(key), value) \ for key, value in replace_items.items())